package com.itkaien.saaexample.controller;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@RestController
public class RestAgent {

    private static final Logger logger = LoggerFactory.getLogger(RestAgent.class);
    private static final String DEFAULT_QUESTION = "你是谁";

    @Autowired
    private ChatModel chatModel;

    @GetMapping("/chat")
    public String doChat() {
        try {
            String question="你好";
            // 输入验证
            if (question == null || question.trim().isEmpty()) {
                question = DEFAULT_QUESTION;
            }

            logger.info("Processing chat request with question: {}", question);
            return chatModel.call(question);
        } catch (Exception e) {
            logger.error("Error occurred while processing chat request: ", e);
            throw new RuntimeException("Failed to process chat request", e);
        }
    }

    @PostMapping("/chatStream")
    public Flux<String> doChatStream(@RequestParam(defaultValue = DEFAULT_QUESTION,name = "question") String question) {
        try {
            // 输入验证
            if (question == null || question.trim().isEmpty()) {
                question = DEFAULT_QUESTION;
            }

            logger.info("Processing chat stream request with question: {}", question);
            return chatModel.stream(question);
        } catch (Exception e) {
            logger.error("Error occurred while processing chat stream request: ", e);
            return Flux.error(new RuntimeException("Failed to process chat stream request", e));
        }
    }




}
